<?php


namespace api\blog\controller;


use api\blog\model\UserModel;
use api\blog\validate\UserValidate;
use cmf\controller\RestBaseController;
use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use think\facade\Db;
use think\Request;

class MoneyController extends RestBaseController
{
    //充值金额
    public function money(Request $request)
    {
        //获取金额
        $money = input('money',0);
        if(!$money)
        {
            $this->error('请输入金额');
        }
        //判断用户
        $user = UserModel::find($request->userId);
        if(!$user)
        {
            $this->error('参数错误');
        }
        $after = $user['wallet'] + $money;
       
        // 启动事务
        Db::startTrans();
        $res = '';
        $log = '';
        try {
            $res = money_log($request->userId,$money,$user['wallet'],$after,'1');
            $log = UserModel::where('id','=',$request->userId)
                ->update(['wallet'=>$after]);
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
        }
        if ($res && $log) {
            // 提交事务
            Db::commit();
            $this->success('充值成功');
        } else {
            // 回滚事务
            Db::rollback();
            $this->error('充值失败');
        }

    }
}